package com.caigou.modules.info.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.modules.info.adapter.InfoPagerAdapter
import com.caigou.modules.info.databinding.FragmentInfoBinding
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@AndroidEntryPoint
class InfoFragment : BaseFragment() {

    private var _binding: FragmentInfoBinding? = null
    private val binding get() = _binding!!

    private val itemNames = mutableListOf(
        "港彩",
        "澳彩",
        "新彩",
        "台彩"
    )
    private val fragments = mutableListOf<Fragment>(
        HkTicketFragment(),
        MacaoTicketFragment(),
        SingaporeTicketFragment(),
        TwTicketFragment()
    )

    private val infoPagerAdapter: InfoPagerAdapter by lazy {
        InfoPagerAdapter(this)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentInfoBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun onBack() {

    }

    override fun initView() {
        binding.infoViewPager2.adapter = infoPagerAdapter
        binding.infoViewPager2.isUserInputEnabled = false
        TabLayoutMediator(binding.infoTabLayout, binding.infoViewPager2, true, true) { tab, position ->
            tab.text = itemNames[position]
        }.attach()
        infoPagerAdapter.setList(fragments)
        binding.infoViewPager2.offscreenPageLimit = itemNames.size
        binding.infoTvMyInfo.setOnClickListener {
            if (AppBackgroundInfo.instance.hasLogin) {
                ARouter.getInstance().build(RouterPath.Info.PAGE_MY_INFO).navigation()
            }
        }
        binding.infoIvBanner.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Info.PAGE_INFO_RULES).navigation()
        }
        binding.infoLlPublish.setOnClickListener {
            ARouter.getInstance().build(RouterPath.Info.PAGE_INFO_PUBLISH).navigation()
        }
    }

    override fun lazyLoad() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}